package com.imooc.forkcgz;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * Created by xyb on 2017/1/11.
 */

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private int type = 0;
    private LinkedHashMap<Integer, String> intBeen;
    private boolean scrollState = false;
    private ArrayList<FibonAsyncTask> fibonAsyncTaskArrayList;
    private boolean isUp = true;


    public ListViewAdapter(Context context, ArrayList<FibonAsyncTask> fibonAsyncTask, LinkedHashMap<Integer, String> intBeen) {
        this.context = context;
        this.intBeen = intBeen;
        this.fibonAsyncTaskArrayList = fibonAsyncTask;
    }

    public boolean isScrollState() {
        return scrollState;
    }

    public void setScrollState(boolean scrollState) {
        this.scrollState = scrollState;
    }

    @Override
    public int getCount() {
        return type;
    }


    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
        notifyDataSetChanged();
    }


    public boolean isUp() {
        return isUp;
    }

    public void setUp(boolean up) {
        isUp = up;
        notifyDataSetInvalidated();
    }

    @Override
    public Object getItem(int position) {
        return intBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.list_item, null);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //  是否滚动
//        if (!scrollState) {
//            Log.e("ListViewAdapter", "没有滚动");
        //为空加入异步任务

        if (isUp) {
            if (intBeen.get(position) == null) {
                addTask(position, viewHolder);
            } else {
                viewHolder.textView.setText(intBeen.get(position) + "下标" + position);
            }
        } else {
            if (intBeen.get(getCount() - position - 1) == null) {
                addTask(getCount() - position - 1, viewHolder);
            } else {
                viewHolder.textView.setText(intBeen.get(getCount() - position - 1) + "下标" + position);
            }

        }
//        } else {
//            Log.e("ListViewAdapter", "正在滚动");
//        }


        return convertView;
    }

    private void addTask(int position, ViewHolder viewHolder) {
        final ViewHolder finalViewHolder = viewHolder;
        FibonAsyncTask fibonAsyncTask = new FibonAsyncTask(intBeen, isUp, new FibonAsyncTask.CallBack() {
            @Override
            public void onCallBack(Integer position) {
                if (isUp) {
                    finalViewHolder.textView.setText(intBeen.get(position).toString() + "下标" + position);
                } else {
                    if (intBeen.get(449 - position) != null) {
                        finalViewHolder.textView.setText(intBeen.get(449 - position).toString() + "下标" + position);
                    }
                }
            }
        }, this);
        this.fibonAsyncTaskArrayList.add(fibonAsyncTask);
        fibonAsyncTask.execute(position);
    }


    static class ViewHolder {
        TextView textView;
    }
}
